package com.rd.controller;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.rd.pojo.Equipment;
import com.rd.service.IDepartmentService;
import com.rd.service.IEquipmentService;
import com.rd.service.INationService;
import com.rd.service.IPoliticsStatusService;
import com.rd.util.ResultModel;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author qiufenfen
 * @since 2023-10-28
 */
@RestController
@RequestMapping("/hrm/equ")
public class EquipmentController {
    @Resource
    private IEquipmentService equipmentService;
    @Resource
    private IPoliticsStatusService politicsStatusService;
    @Resource
    private INationService nationService;
    @Resource
    private IDepartmentService departmentService;

    @ApiOperation(value = "获取所有设备（分页）")
    @GetMapping("/empList")
    public ResultModel getEquipment(@RequestParam(defaultValue = "1") Integer currentPage,
                                    @RequestParam(defaultValue = "10") Integer size,
                                    Equipment equipment,
                                    LocalDate[] beginDateScope){
        return equipmentService.getEquipmentByPage(currentPage,size,equipment,beginDateScope);
    }


    @ApiOperation(value = "添加员工")
    @PostMapping("/addEmp")
    public ResultModel addEmp(@RequestBody Equipment equipment){
        //需要计算合同期限,所以
        return equipmentService.addEmp(equipment);
    }

    @ApiOperation(value = "更新员工")
    @PostMapping("/updateEmp")
    public ResultModel updateEmp(@RequestBody Equipment equipment){
        if(equipmentService.updateById(equipment)){
            return ResultModel.createBySuccess("更新成功！");
        }
        return ResultModel.createByErrorCodeMessage("更新失败！");
    }

    @ApiOperation(value = "删除员工")
    @GetMapping("/deleteEmp/{id}")
    public ResultModel deleteEmp(@PathVariable Integer id){
        if(equipmentService.removeById(id)){
            return ResultModel.createBySuccess("删除成功！");
        }
        return ResultModel.createByErrorCodeMessage("删除失败！");
    }

    @ApiOperation(value = "导出员工数据")
    /** produces = "application/octet-stream" 附件*/
    @GetMapping(value = "/export",produces = "application/octet-stream")
    public void exportEquipment(HttpServletResponse response){
        System.out.println("导出数据");
        // 不分页
        List<Equipment> list = equipmentService.getEquipment(null);
        ExportParams params = new ExportParams("员工表","员工表", ExcelType.HSSF);
        //工作簿 （导出excel表格）
        Workbook workbook = ExcelExportUtil.exportExcel(params,Equipment.class,list);
        ServletOutputStream out = null;
        try {
            //流形式
            response.setHeader("content-type","application/octet-stream");
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            //防止中文乱码
            response.setHeader("content-disposition","attachment;filename="+
                    URLEncoder.encode("员工表.xls","UTF-8"));
            out = response.getOutputStream();
            workbook.write(out);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
